/**
 * 防抖函数-只响应最后一次
 * @param {Function} fn
 * @param {Number} cd
 * @returns {Function}
 */
export const debounce = function (fn, cd) {
  let t = null;
  if (!cd || cd < 0 || Number.isNaN(parseInt(cd))) {
    cd = 300;
  }

  return function (...args) {
    // eslint-disable-next-line @typescript-eslint/no-this-alias
    const _this = this;
    t && clearTimeout(t);
    t = setTimeout(() => {
      return fn.apply(_this, args);
    }, cd);
  };
}
